package factorytest.iflytek.com.einkiflytekfactorytest.factorytestui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.FileReadWrite;

public class DspTestActivity extends FactoryBaseActivity implements View.OnClickListener {
    private TextView txtLog;
    private volatile boolean isStarted;

    @Override
    public int getLayoutId() {
        return R.layout.frgment_engineer_xfm10213;
    }

    @Override
    public void onInitView(@Nullable Bundle savedInstanceState) {
        txtLog = (TextView) findViewById(R.id.txtLog);

        findViewById(R.id.btnStart).setOnClickListener(this);
        findViewById(R.id.btnStop).setOnClickListener(this);
        findViewById(R.id.btnExit).setOnClickListener(this);

    }

    private final static int MSG_UPDATE_LOG = 100;
    private final static int MSG_UPDATE_WAKEUP_INFO = 101;
    private final static int PERIOD_UPDATE_WAKEUP_INFO = 1000;

    @Override
    public void onHandleMessage(Handler handler, Message msg) {

        switch (msg.what) {
            case MSG_UPDATE_LOG:
                doUpdateLog((String) msg.obj);
                break;
            case MSG_UPDATE_WAKEUP_INFO:
                updateLog(readWakeupInfo());
                handler.sendEmptyMessageDelayed(MSG_UPDATE_WAKEUP_INFO, PERIOD_UPDATE_WAKEUP_INFO);
                break;
            default:
                break;
        }
    }

    private void updateLog(String log) {
        getHandler().obtainMessage(MSG_UPDATE_LOG, log).sendToTarget();
    }

    private void doUpdateLog(String log) {
        String txt = txtLog.getText().toString();
        if (txt.length() > 1024 * 6) {
            txtLog.setText(log);
        } else {
            txtLog.setText(txt + "\n" + log);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStart:
                if (isStarted) {
                    updateLog("DSP唤醒测试 已经开始");
                    break;
                }
                isStarted = true;

                txtLog.setText("");
                updateLog("开始DSP唤醒测试");
                startTest();
                break;
            case R.id.btnStop:
                updateLog("停止DSP唤醒测试");
                stopTest();
                isStarted = false;
                break;
            case R.id.btnExit:
                stopTest();
                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(this);
                break;
        }
    }

    /////////////////////
    private final static String PATH_WAKEUP = "/sys/class/xfm10213/wakeup";

    private String readWakeupInfo() {
        return FileReadWrite.readFileByLines(PATH_WAKEUP);
    }

    private void startTest() {
        FileReadWrite.writeStringToFile(PATH_WAKEUP, "1");
        getHandler().sendEmptyMessage(MSG_UPDATE_WAKEUP_INFO);
    }

    private void stopTest() {
        FileReadWrite.writeStringToFile(PATH_WAKEUP, "0");
        getHandler().removeMessages(MSG_UPDATE_WAKEUP_INFO);
    }

}
